// Package user 存放用户 Model 相关逻辑
package user

import (
	"gohub/app/models"
	"gohub/pkg/database"
	"time"
)

// User 用户模型
type User struct {
	models.BaseModel

	Username string `json:"username,omitempty"`

	City         string `json:"city,omitempty"`
	Introduction string `json:"introduction,omitempty"`
	Avatar       string `json:"avatar,omitempty"`
	Mobile       string `json:"mobile,omitempty"`

	Email    string `json:"-"`
	Password string `json:"-"`
	RoleId   string `json:"role"`

	models.CommonTimestampsField
}

// func (User) TableName() string {
// 	return "admin_user"
// }

// Create 创建用户，通过 User.ID 来判断是否创建成功
func (userModel *User) Create() {
	database.DB.Create(&userModel)
}

// ComparePassword 密码是否正确
func (userModel *User) ComparePassword(_password string) bool {
	//return hash.BcryptCheck(_password, userModel.Password)
	return _password == userModel.Password
}

func (userModel *User) Save() (rowsAffected int64) {
	result := database.DB.Save(&userModel)
	return result.RowsAffected
}

func (userModel *User) Delete() (rowsAffected int64) {
	result := database.DB.Delete(&userModel)
	return result.RowsAffected
}

// GetCreatedAtString 获取格式化的创建时间字符串
func (u *User) GetCreatedAtString() string {
	if u.CreatedAt == nil {
		return ""
	}
	return time.Time(*u.CreatedAt).Format("2006-01-02 15:04:05")
}
